package cn.doitedu.smart_marketing.main;

import cn.doitedu.datagen.LogBean;
import cn.doitedu.smart_marketing.beans.RuleMatchResult;
import cn.doitedu.smart_marketing.functions.RuleProcessor;
import cn.doitedu.smart_marketing.sources.KafkaSourceBuilder;
import com.alibaba.fastjson.JSON;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.Collector;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2021/7/16
 **/
public class Main {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(1);

        KafkaSourceBuilder kafkaSourceBuilder = new KafkaSourceBuilder();
        FlinkKafkaConsumer<String> eventSource = kafkaSourceBuilder.<String>build("yinew_applog");

        DataStream<String> ds = env.addSource(eventSource);

        DataStream<LogBean> beanSource = ds.map(s -> JSON.parseObject(s, LogBean.class));

        DataStream<RuleMatchResult> result = beanSource.keyBy(b -> b.getAccount())
                .process(new RuleProcessor());


        result.print();


        env.execute();

    }
}
